package org.samcolon.eeexplore.demo;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.bean.BeanDesc;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.PropDesc;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import org.samcolon.eeexplore.entity.SysMenu;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Description: 实体信息读取，依赖于mybatis plus，生成数据备份文件
 * @author: Sam.zhang
 * @date: 2021/11/29 - 11:13
 */
public class MybatisPlusBackupDemo {

    public static void main(String[] args) {
        String tableName = AnnotationUtil.getAnnotationValue(SysMenu.class, TableName.class);
        Console.log("tableName: {}", tableName);

        BeanDesc beanDesc = BeanUtil.getBeanDesc(SysMenu.class);
        List<String> fieldList = CollUtil.newArrayList();
        List<String> tableFieldList = CollUtil.newArrayList();
        List<String> insertList = CollUtil.newArrayList();

        Collection<PropDesc> props = beanDesc.getProps();
        int fieldNum = 0;
        for (PropDesc prop : props) {
            String fieldName = null;
            Field field = prop.getField();
            if (AnnotationUtil.hasAnnotation(field, TableField.class)) {
                fieldName = AnnotationUtil.getAnnotationValue(field, TableField.class);
            } else if (AnnotationUtil.hasAnnotation(field, TableId.class)) {
                fieldName = AnnotationUtil.getAnnotationValue(field, TableId.class);
            }
            Console.log("field: {}\t type: {}\t table field: {}", prop.getFieldName(), prop.getFieldClass().getName(), fieldName);

            fieldList.add(prop.getFieldName());
            tableFieldList.add(fieldName);
            fieldNum++;
        }

        List<SysMenu> testData = getTestData();
        for (SysMenu testDatum : testData) {
            StringBuilder sqlBuilder = StrUtil.builder("INSERT INTO `");
            sqlBuilder.append(tableName).append("`(`");
            sqlBuilder.append(StrUtil.join("`,`", tableFieldList.toArray(new String[]{})));
            sqlBuilder.append("`) VALUES(");

            for (int i = 0; i < fieldNum; i++) {
                Object property = BeanUtil.getProperty(testDatum, fieldList.get(i));

                if (i > 0) sqlBuilder.append(",");
                if (property == null) {
                    sqlBuilder.append("NULL");
                } else if (property instanceof Integer) {
                    sqlBuilder.append(property);
                } else if (property instanceof String) {
                    sqlBuilder.append("'").append(property).append("'");
                } else if (property instanceof Date) {
                    sqlBuilder.append("'").append(DateUtil.formatDateTime((Date) property)).append("'");
                }
            }
            sqlBuilder.append(");");

            insertList.add(sqlBuilder.toString());
        }
        insertList.forEach(item -> Console.log(item));
    }

    private static List<SysMenu> getTestData() {
        List<SysMenu> list = CollUtil.newArrayList();
        for (int i = 0; i < 20; i++) {
            list.add(new SysMenu(i, i, "name" + i, "permission" + i, "path" + i, i, "icon" + i, i, i, i, new Date(), new Date(), i, i, i, i, String.valueOf(i), i, "config" + i, "viewPath" + i, "title" + i, "redirect" + i, i, i));
        }
        return list;
    }
}
